﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace SXFramWork.Core
{
    public class ConnectionFactory
    {
        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="strConn">数据库连接字符串</param>
        /// <returns>数据库连接</returns>
        public static IDbConnection CreateConnection(DatabaseType dbType, string strConn)
        {
            IDbConnection connection = null;
            if (strConn.IsNull())
                throw new ArgumentNullException("缺少数据库连接字符串，无法创建Connection");
            switch (dbType)
            {
                case DatabaseType.SqlServer:
                    connection = new SqlConnection(strConn);
                    break;
                //case DatabaseType.MySQL:
                //    connection = new MySqlConnection(strConn);
                //    break;

                default:
                    throw new ArgumentNullException($"还不支持的{dbType.ToString()}数据库类型");

            }
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
            return connection;
        }

    }
}
